//接口地址
var url="http://47.105.48.86:8081/ad-proxy";

//	获取地址栏参数
function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

//	错误提示
function isErr(res) {
	layer.msg(res.msg);
	if(res.code == "0020"){
		location.href="login.html";
	}
}
//	获取token
//var token=GetQueryString("token");

var token = sessionStorage.getItem("token");

// if(token == "" || token == null || token == undefined){
// 	location.href='login.html';
// }
//var token_id=GetQueryString("id");
var token_id = sessionStorage.getItem("userId");
var token_str="?token="+token+"&id="+token_id;
$(".href_url").on("click",function(){
	location.href=$(this).attr("data")+token_str;
})
function logout(){
	$.ajax({
		url: url + "/sys/logout",
		type: 'Get',
		//		参数需转为字符串
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",
		useDefaultXhrHeader: false,
		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			if(result.code == '0') {
				layer.msg('退出成功',{time:2*1000},function() {
					window.location.href='../../public/login.html';
				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

function resetPWD(){
    var oldPasswd=$(".oldPasswd").val();
    var newPasswd=$(".newPasswd").val();
    var newPasswd2=$(".newPasswd2").val();
    if(!oldPasswd){
    	layer.msg("请输入旧密码");
    	return false;
    }
    if(!newPasswd){
    	layer.msg("请输入新密码");
    	return false;
    }
    if(!newPasswd2){
    	layer.msg("请再次输入密码");
    	return false;
    }
    if(newPasswd != newPasswd2){
    	layer.msg("两次密码输入不一致");
    	return false;
    }
    var d={};
    d.oldPassword=oldPasswd;
    d.newPassword=newPasswd;
    $.ajax({
		url: url + "/sys/password",
		type: 'POST',
		data: JSON.stringify(d),
		dataType: 'json',
		contentType: "application/json",
		
		beforeSend: function(XMLHttpRequest) {
//			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				layer.msg(result.msg,{time:2*1000},function() {
					$("#pwdModal").modal("hide");
					$(".oldPasswd").val("");
				    $(".newPasswd").val("");
				    $(".newPasswd2").val("");
				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}


//title弹框
function tips(event,str) {
    layer.tips(str, $(event.target), {
        time: 2000,
        tips: [1, '#fffee3']
    });
}
//点击退出按钮
$(".user-list").children().next("li").on("click",function(){
	logout();
});


//获取当前广告主的用户余额

function getBalance(){
	$.ajax({
		url: url+'/sys/account',
		type: "post",
		dataType: 'json',
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
		
			if(result.code == '0') {
				$('.user-list').children().first().html("余额："+result.data.amount);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}



var dataResult = "";
//封装的ajax方法---全局调用---
function getAgentMsg(param,method,datas){
	$.ajax({
		url: url + param,
		type: method,
		dataType: 'json',
		data:JSON.stringify(datas),
		async:false,
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			if(result.code == '0') {
				dataResult = result;
//				console.log(dataResult);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

//获取当前广告主账号基本信息
function getAgUserInfo(){
	$.ajax({
		url: url+'/advertiser/adUserInfo/queryInfo',
		type: "get",
		dataType: 'json',
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			if(result.code == 0) {
				$('.user-info').children().first().text(result.data.concat);
				$('.user-info').children().first().next().text(result.data.adComapyName);
			} 
		}
	});
}
getBalance();
getAgUserInfo();

//备用的ajax2.
function getAgentMsg2(param2,method2,datas2){
	$.ajax({
		url: url + param2,
		type: method2,
		dataType: 'json',
		data:JSON.stringify(datas2),
		async:false,
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
//		console.log(result);
			if(result.code == '0') {
				dataResult2 = result;
//				layer.msg('退出成功',{time:2*1000},function() {
//					location.href='login.html';
//				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}